﻿using System;
using System.Web.Services;
using Microsoft.SharePoint.Administration;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using System.Web;
using System.Diagnostics;

namespace ServBus.ListForm2010.WSLibrary
{
    /// <summary> 
    /// Summary description for Service1 
    /// </summary> 
    ///[WebService(Namespace = "http://ServBus.com/ListForm2010")]
    [WebService(Namespace = "http://ServBus.com/ListForm2010/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
    [System.ComponentModel.ToolboxItem(false)]    
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
    // [System.Web.Script.Services.ScriptService] 
    public class FarmUpdate : System.Web.Services.WebService
    {
        [WebMethod]
        public void updateFarmProperties(string base64, string propertyKey)
        {

            HttpContext.Current.Items["FormDigestValidated"] = true;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    SPFarm _farm = SPFarm.Local;
                    if (_farm != null)
                    {
                        _farm.Properties[propertyKey] = base64;
                        _farm.Update();
                    }
                    //Write("FarmProperties:ok", EventLogEntryType.Warning);
                }
                catch (Exception ex)
                {
                    Write("FarmProperties:" + ex.ToString(), EventLogEntryType.Warning);
                }
            });
            HttpContext.Current.Items["FormDigestValidated"] = false;
        }

        public static void Write(string message, EventLogEntryType eventType)
        {
            EventLog log = new EventLog();
            log.Source = "InfoBus ListForm";
            log.WriteEntry(message, eventType);
        }

        [WebMethod]
        public void updateAppProperties(string base64, string propertyKey, Guid appID)
        {

            HttpContext.Current.Items["FormDigestValidated"] = true;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    SPWebApplication _webapp = SPWebService.ContentService.WebApplications[appID];
                    if (_webapp != null)
                    {
                        _webapp.Properties[propertyKey] = base64;
                        _webapp.Update();
                    }
                    //Write("WebProperties_ok:"+base64, EventLogEntryType.Warning);
                }
                catch (Exception ex)
                {
                    Write("WebappProperties:" + ex.ToString(), EventLogEntryType.Warning);
                }
            });
            HttpContext.Current.Items["FormDigestValidated"] = false;
        }
    } 
}
